<template>
    <a-card :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
            <a-form layout="inline" @keyup.enter.native="searchQuery">
                <a-row :gutter="24">
                    <a-col :md="6" :sm="8">
                        <a-form-item label="年度">
                            <!-- <a-date-picker v-model="queryParam.shijian" /> -->
                            <a-date-picker
                                :open='yearShowOne'
                                @openChange="openChangeOne"
                                @panelChange="panelChangeOne"
                                mode="year"
                                format="YYYY"
                                type="date"
                                :default-value="moment(new Date(), 'YYYY')"
                                v-model="queryParam.sbnd"
                                suffix-icon=" "
                                placeholder="年份"
                            />
                        </a-form-item>
                    </a-col>
                    <a-col :md="6" :sm="8">
                        <a-form-item label="推荐获奖等级">
                            <a-select v-model="queryParam.jxdj" placeholder="请选择奖项等级" @change="handleSelectJxdj">
                                <!-- <a-select-option :value="0">创新大奖</a-select-option> -->
                                <a-select-option value="1">一等奖</a-select-option>
                                <a-select-option value="2">二等奖</a-select-option>
                              <!--
                                <a-select-option value="3">三等奖</a-select-option>
                                -->
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="6" :sm="8">
                        <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                            <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                            <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                            <!-- <a-button type="primary" @click="downloadAllTjb" style="margin-left: 8px;float: right;">打印</a-button> -->
                        </span>
                    </a-col>
                </a-row>
            </a-form>
        </div>
        <!-- 查询区域-END -->
        <!-- 操作按钮区域 -->
        <div class="table-operator"></div>
        <!-- table区域-begin -->
        <div>
            <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
                <i class="anticon anticon-info-circle ant-alert-icon"></i>已选择&nbsp;<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
                <a style="margin-left: 24px" @click="onClearSelected">清空</a>
            </div> -->
            <a-table
                ref="table"
                size="middle"
                :scroll="{x:1275}"
                bordered
                rowKey="id"
                :columns="columns"
                :dataSource="dataSource"
                :pagination="ipagination"
                :loading="loading"
                class="j-table-force-nowrap"
                :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
                @change="handleTableChange">
                <span slot="action" slot-scope="text, record">
                    <!-- <a @click="viewFile(record)">查看</a>
                    <a-divider type="vertical" />
                    <a @click="handelDownload(record)" class="ant-dropdown-link">下载</a> -->
                    <a>打印</a>
                </span>
            </a-table>
        </div>
        <cxj-tj-tjyj-modal ref="modalForm" @ok="modalFormOk"></cxj-tj-tjyj-modal>
        <!-- 预览 -->
        <testPdf :sys_cxj_jttjdw_id="sys_cxj_jttjdw_id" :sbnd="shijian" :showPdf="showpdf"></testPdf>
    </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less';
import { mixinDevice } from '@/utils/mixin';
import { JeecgListMixin } from '@/mixins/JeecgListMixin';
import CxjTjTjyjModal from './modules/CxjTjTjyjModal';
import { cxjsbGetSbtjdw } from '@/api/shenbao';
import { getAction } from '@/api/manage';
import { ACCESS_TOKEN } from "@/store/mutation-types";
import Vue from 'vue'
import moment from 'moment'
import testPdf from "./testPdf"
import {
    cxjTjbListGly
} from "@/api/api"
import {
    cxjTjbDownload,
    cxjTjbDownloadAll
} from "@/api/manage"
export default {
    name: 'DownloadTjb',
    // mixins:[JeecgListMixin, mixinDevice],
    mixins:[mixinDevice],
    components: {
        CxjTjTjyjModal,
        testPdf
    },
    data () {
        return {
            sys_cxj_jttjdw_id: "",
            shijian: "",
            yearShowOne:false,
            showpdf: false,
            fileList: [],
            queryParam: {},
            description: '成果推荐表管理页面',
            // 表头
            columns: [
                {
                    title: '序号',
                    align: "center",
                    dataIndex: '',
                    width: 50,
                    customRender: function (t,r,index) {
                        return parseInt(index)+1;
                    }
                },
                {
                    title:'奖项',
                    align:"center",
                    dataIndex: 'xm_hjdj_show',
                    // width: 150
                },
                {
                    title:'专家姓名',
                    align:"center",
                    dataIndex: 'zj_xm',
                    // width: 150
                },
                {
                    title:'手机号',
                    align:"center",
                    dataIndex: 'zj_sj',
                    // width: 150
                },

                {
                    title:'成果数量',
                    align:"center",
                    dataIndex: 'cgsl',
                    // width: 150
                },
                {
                    title:'已投成果数量',
                    align:"center",
                    dataIndex: 'ytpsl',
                    // width: 150
                },
                /* {
                    title:'是否投票',
                    align:"center",
                    dataIndex: '',
                    // width: 150
                },
                {
                    title: '操作',
                    dataIndex: 'action',
                    align: "center",
                    fixed: "right",
                    width: 130,
                    scopedSlots: { customRender: 'action' }
                } */
            ],
            url: {
                list: "/cxj/cxjWysdTpZgcg/cxjwysd_zjtpqk",
                importExcelUrl: "/cxj/cxjTjTjyj/cxjgl_cxjTjbDownload_gly",
            },
            dictOptions:{},
            superFieldList:[],
            fileStatus: "",
            sbnd: moment(new Date()).format('YYYY'),
            selectedRowKeys: [],
            selectionRows: [],
            loading: false,
            isorter: {
                column: 'createTime',
                order: 'desc',
            },
            dataSource: [],
            ipagination: {
                current: 1,
                pageSize: 10,
                pageSizeOptions: ['10', '20', '30'],
                showTotal: (total, range) => {
                    return range[0] + "-" + range[1] + " 共" + total + "条"
                },
                showQuickJumper: true,
                showSizeChanger: true,
                total: 0
            },
        }
    },
    created() {
        this.$set(this.dictOptions, 'isValid', [{text:'是',value:'Y'},{text:'否',value:'N'}])
        this.getSuperFieldList();
        this.queryParam.jxdj = "1"
        this.loadData();
    },
    computed: {
        importExcelUrl: function(){
            return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
        },
    },
    methods: {
        moment,
        handleSelectJxdj(value){
            this.queryParam.jxdj = value;
            this.loadData();
        },
        searchQuery() {
            this.loadData(1);
        },
        getQueryField() {
            //TODO 字段权限控制
            var str = "id,";
            this.columns.forEach(function (value) {
                str += "," + value.dataIndex;
            });
            return str;
        },
        loadData(arg) {
            if (arg === 1) {
                this.ipagination.current = 1;
            }
            this.loading = true;
            this.queryParam.shijian = this.sbnd;
            let params = Object.assign(this.queryParam, this.isorter, this.filters)
            params.field = this.getQueryField();
            params.pageNo = this.ipagination.current;
            params.pageSize = this.ipagination.pageSize;
            getAction(this.url.list, params).then(res=>{
                if (res.success) {
                    this.dataSource = res.result.records || res.result;
                    if (res.result.total) {
                        this.ipagination.total = res.result.total;
                    } else {
                        this.ipagination.total = 0;
                    }
                }
                if (res.code === 510) {
                    this.$message.warning(res.message)
                }
                this.loading = false;
            })
        },
        openChangeOne(status){
            //status是打开或关闭的状态
            if(status){
                this.yearShowOne =  true;
            }
        },
        // 得到年份选择器的值
        panelChangeOne(value){
            // params.sbnd = value;
            this.sbnd = moment(value).format('YYYY');
            this.queryParam.sbnd = this.sbnd;
            this.yearShowOne = false
        },
        searchReset() {
            this.sbnd = moment(new Date()).format('YYYY');
            this.queryParam.sbnd = this.sbnd;
            this.loadData(1);
        },
        dataCustomRequest(file){
            let fileList = [...this.fileList, file.file]
            this.fileList = fileList.slice(-1);
            this.form.file = this.fileList[0];
            return false;
        },
        onSelectChange(selectedRowKeys, selectionRows) {
            this.selectedRowKeys = selectedRowKeys;
            this.selectionRows = selectionRows;
        },
        onClearSelected() {
            this.selectedRowKeys = [];
            this.selectionRows = [];
        },
        handleTableChange(pagination, filters, sorter) {
            //分页、排序、筛选变化时触发
            //TODO 筛选
            if (Object.keys(sorter).length > 0) {
                this.isorter.column = sorter.field;
                this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
            }
            this.ipagination = pagination;
            this.loadData();
        },
        modalFormOk() {
            // 新增/修改 成功时，重载列表
            this.loadData();
            //清空列表选中
            this.onClearSelected()
        },
        beforeUpload(file) {
            if (file) {
                if (file.size > 50*1024*1024) { // 此处判断上传文件的大小 , 单位为 b 故要乘以两个1024
                    this.$message.warning('文件最大为50兆')
                    this.fileList.
                    return;
                } else if (file.type!='application/pdf') { // 此处判断上传文件的格式
                    this.$message.warning('请上传pdf格式的文件')
                    return;
                }
            }
        },
        initDictConfig(){},
        getSuperFieldList(){
            let fieldList=[];
            fieldList.push({type:'string',value:'cxj_xmbh',text:'推荐单位类型',dictCode:''})
            fieldList.push({type:'string',value:'cxj_mc_zw',text:'推荐单位',dictCode:''})
            fieldList.push({type:'string',value:'fl_id',text:'总数量',dictCode:''})
            fieldList.push({type:'string',value:'sbdw_qymc',text:'未推荐',dictCode:''})
            fieldList.push({type:'string',value:'cxj_qssj',text:'同意推荐',dictCode:''})
            fieldList.push({type:'string',value:'cxj_wcsj',text:'不同意推荐',dictCode:''})
            fieldList.push({type:'string',value:'wcrlist',text:'退回修改',dictCode:''})
            fieldList.push({type:'string',value:'wcdwlist',text:'退回提交待推荐',dictCode:''})
            fieldList.push({type:'string',value:'tjzt',text:'退回推荐',dictCode:''})
            fieldList.push({type:'switch',value:'isValid',text:'是否有效'})
            this.superFieldList = fieldList;
        },
        viewFile(data){
            this.showpdf = !this.showpdf;
            this.sys_cxj_jttjdw_id = data.sys_cxj_jttjdw_id;
            this.shijian = data.shijian;
        },
        downloadAllTjb(){
            let that = this;
            let date = new Date();
            let y = date.getFullYear();
            let data = {
                sbnd: y
            }
            cxjTjbDownloadAll(data).then(res=>{
                // const blob = new Blob([res], { type: 'application/zip' })
                // const url = window.URL.createObjectURL(blob)
                // const link = document.createElement('a') // 创建a标签
                // link.href = url
                // link.download = "推荐表" // 文件重命名，若无需重命名，将该行删去
                // link.click()
                // URL.revokeObjectURL(url);
                console.log(res)
                const _res = res;
                let blob = new Blob([_res], {type: 'application/zip'});
                let downloadElement = document.createElement("a");
                let href = window.URL.createObjectURL(blob); //创建下载的链接
                downloadElement.href = href;
                downloadElement.download = "推荐表"; //下载后文件名
                document.body.appendChild(downloadElement);
                downloadElement.click(); //点击下载
                document.body.removeChild(downloadElement); //下载完成移除元素
                window.URL.revokeObjectURL(href); //释放掉blob对象
            })
        },
        //  url, zlMc
		handelDownload(record){
			let that = this;
            let data = {
                sys_cxj_jttjdw_id: record.sys_cxj_jttjdw_id,
                sbnd: record.shijian
            }
            cxjTjbDownload(data).then(res=>{
                that.downfn('成果推荐文件',res);
            })
        },
		downfn(name, res) {
            const _res = res;
            let blob = new Blob([_res], {type: 'application/pdf'});
            let downloadElement = document.createElement("a");
            let href = window.URL.createObjectURL(blob); //创建下载的链接
            downloadElement.href = href;
            downloadElement.download = name; //下载后文件名
            document.body.appendChild(downloadElement);
            downloadElement.click(); //点击下载
            document.body.removeChild(downloadElement); //下载完成移除元素
            window.URL.revokeObjectURL(href); //释放掉blob对象
        }
    }
}
</script>
<style scoped lang="less">
    @import '~@assets/less/common.less';
    .table-operator {
        span {
            color: #252525;
            line-height: 20px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-size: 14px;
            display: inline-block;
            margin-right: 18px;
        }
    }
    /deep/.ant-table-thead > tr > th .ant-table-header-column {
      width:100%;
      text-align: center!important;
    }
    /deep/.ant-table-tbody > tr > td {
      word-break: break-all;
      white-space: normal;
      word-wrap:break-word;
      font-size:14px;
      padding:3px 3px!important;
    }
    /deep/.ant-table-tbody > tr > td span{
      font-size:14px;
    }
</style>